﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nova_Common;
using Nova_MOD;
using Nova_BLL;
using Nova_DAL;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;
using System.Text;
using System.Globalization;
using System.IO;


public partial class Report_JoinRep : System.Web.UI.Page
{
    DbHelper access = new DbHelper();
    DbCommand cmd = null;
    static DataSet ds = null;
    string strSQL = "";

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        bind();
    }

    protected void bind()
    {
        this.strSQL = @"SELECT  TBM.Mat_Name,TAD.Mat_Code AS Mat_Code,TAD.StockNo AS StockNo,TAD.Num,ISNULL(TAD.Num,0)-ISNULL(TAD.Act_Num,0) as YNUM,TBM.Unit_Base,TAD.Act_Num,TAD.CDT,TBA.OID as OID  
                    FROM TAddStorage_List TAD 
                    LEFT JOIN TBase_Material TBM ON TBM.Mat_Code = TAD.Mat_Code 
                    LEFT JOIN TBill_AddStorage TBA ON TBA.StockNo=TAD.StockNo
                    WHERE 1=1  AND TAD.Sec_Mat_ID<>0";

        if (this.txtStockNo.Text != "")
        {
            strSQL = strSQL + "AND TAD.StockNo Like '%" + txtStockNo.Text.Trim() + "%'";
        }
        if (this.txtMat_Code.Text != "")
        {
            this.strSQL = this.strSQL + " AND TAD.Mat_Code LIKE '%" + this.txtMat_Code.Text.ToString().Trim() + "%'";
        }
        if (this.txtMat_Name.Text != "")
        {
            this.strSQL = this.strSQL + " AND TBM.Mat_Name LIKE '%" + this.txtMat_Name.Text.ToString().Trim() + "%'";
        }
        if (this.getDataTime1.BegTime != "")
        {
            this.strSQL = this.strSQL + " AND TAD.CDT >= convert(datetime,'" + this.getDataTime1.BegTime.ToString() + "')";
        }
        if (this.getDataTime1.EndTime != "")
        {
            this.strSQL = this.strSQL + " AND TAD.CDT <=convert(datetime,'" + this.getDataTime1.EndTime.ToString() + "')";
        }
        if (this.ddlStatus.SelectedValue == "1")
        {
            this.strSQL = this.strSQL + " AND SUBSTRING(TAD.StockNo,1,2) != 'EA'";
        }
        else if (this.ddlStatus.SelectedValue == "2")
        {
            this.strSQL = this.strSQL + " AND SUBSTRING(TAD.StockNo,1,2) = 'EA'";
        }
        this.strSQL = this.strSQL + " ORDER BY TAD.CDT DESC";
        this.cmd = this.access.GetSqlStringCommond(this.strSQL);
        ds = this.access.ExecuteDataSet(this.cmd);
        this.gv.DataSource = ds.Tables[0].DefaultView;
        this.gv.DataBind();
    }

    public override void VerifyRenderingInServerForm(Control control)
    { }

    protected void ibtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        //Response.Clear();
        //Response.BufferOutput = true;
        ////设定输出的字符集(就是简体中文集)
        //Response.Charset = "utf-8";
        ////导出的文件名为score.xls
        //DateTime dtime = DateTime.Now;

        //Response.AppendHeader("Content-Disposition", "attachment;filename=" + dtime.ToString("yyyyMMddHHmmss") + ".xls");
        ////指定的数据是否是ASCII或一种Unicode 变体
        //Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        ////设置导出文件的格式
        //Response.ContentType = "application/ms-excel";
        ////关闭ViewState
        //EnableViewState = false;
        //System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
        //System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
        //System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
        //gv.RenderControl(textWriter);
        ////把HTML写回浏览器
        //Response.Write(stringWriter.ToString());
        //Response.End();

        DataTable dt = ds.Tables[0];
        Response.Clear();
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Charset = "GB2312";
        Response.AppendHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode("入库报表" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"));
        Response.ContentType = "application/ms-excel";

        //Response.Write("物料编码\t物料名称\t单位\t类型\t移动类型\t单据数量\t实入数量\t数量差\t凭证号\t库存标示\t库存编号\t分管用户\t是否固定资产\t操作用户\t操作时间");
        Response.Write("物料编码\t物料名称\t单位\t单据数量\t实入数量\t数量差\t操作用户\t操作时间");

        Response.Write(Environment.NewLine);
        foreach (DataRow dr in dt.Rows)
        {

            StringBuilder sb = new StringBuilder();

            //sb.Append(dr[1] + "\t" + dr[0] + "\t" + dr["Unit_Base"] + "\t" + dr["TS02"] + "\t" + dr["V02"] + "\t" + dr["Num"] + "\t" + dr["Act_Num"] + "\t" + dr["YNUM"] + "\t" +  dr["V01"] + "\t" + dr["S01"] + "\t" + ("" + dr["S02"]) + "\t" + dr["TS01"] + "\t" + dr["S10"] + "\t" + dr["OID"] + "\t" + dr["CDT"]);
            sb.Append(dr[1] + "\t" + dr[0] + "\t" + dr["Unit_Base"] + "\t" + dr["Num"] + "\t" + dr["Act_Num"] + "\t" + dr["YNUM"] + "\t" + dr["OID"] + "\t" + dr["CDT"]);
            Response.Write(sb.ToString());
            Response.Write(Environment.NewLine);

        }
        Response.End();
    }
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gv.PageIndex = e.NewPageIndex;
        this.bind();
    }

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowIndex != -1)
            {
                LinkButton lbtn_StockNo = (LinkButton)e.Row.FindControl("lbtn_StockNo");

                string updateUrl = string.Format("javascript: document.getElementById('{1}').value=window.showModalDialog('RepList.aspx?Rom=" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "&strStockNo={0}', window, 'dialogWidth=800px;dialogHeight=500px;scroll:no;resizable:no;status:0;');document.URL=location.href;", lbtn_StockNo.CommandArgument.ToString(), hidden.ClientID);
                //string updateUrl = string.Format("javascript: document.getElementById('{2}').value=window.showModalDialog('UnDelDataList.aspx?Rom=" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "&strStockNo={0}&strAA={1}', window, 'dialogWidth=800px;dialogHeight=500px;scroll:no;resizable:no;status:0;');document.URL=location.href;", lbtn_StockNo.CommandArgument.ToString(), gv.DataKeys[e.Row.RowIndex].Value.ToString(), hidden.ClientID);


                lbtn_StockNo.Attributes.Add("onclick", updateUrl);

            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (Convert.ToDecimal(e.Row.Cells[7].Text.ToString()) > 0)
                //找到库存的索引，设定数量大于0时变颜色
                {
                    e.Row.Cells[7].ForeColor = System.Drawing.Color.Red;
                    //设计颜色变化为红色
                }
            }

        }
        catch (Exception ex)
        {

        }
    }
}
